package com.sxfq.loansupermarket.backsystem.modular.websocket;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class OutputTest {
    public static void main(String[] args) {
//        String command = "C:\\Users\\dengze\\Desktop\\add.py";  // python程序的路径
//        String str1 = "1";  // 传入python的参数1
//        String str2 = "2";  // 传入python的参数2
//        String[] cmd = new String[]{"python", command, str1, str2};
//        getCmdOutput(cmd);  // 通过Runtime.getRuntime()执行脚本文件
        System.out.println("111");
    }

//    public static void main(String[] args) {
//        String jsapi_ticket = "sM4AOVdWfPE4DxkXGEs8VKlzVeT6rkBB2vv4rDxlcK7eCBAiK8k_ZiYM31di8pplrMJdVx4jIo-VuRL3S_g19A";
//        // 注意 URL 一定要动态获取，不能 hardcode
//        String url = "http://example.com";
//        Map<String, String> ret = sign(jsapi_ticket, url);
//        for (Map.Entry entry : ret.entrySet()) {
//            System.out.println(entry.getKey() + ", " + entry.getValue());
//        }
//    };

    public static Map<String, String> sign(String jsapi_ticket, String url) {
        Map<String, String> ret = new HashMap<String, String>();
        String nonce_str = create_nonce_str();
        String timestamp = create_timestamp();
        String string1;
        String signature = "";
        //注意这里参数名必须全部小写，且必须有序
        string1 = "jsapi_ticket=" + jsapi_ticket +
                "&noncestr=" + nonce_str +
                "&timestamp=" + timestamp +
                "&url=" + url;
        System.out.println(string1);
        try
        {
            MessageDigest crypt = MessageDigest.getInstance("SHA-1");
            crypt.reset();
            crypt.update(string1.getBytes("UTF-8"));
            signature = byteToHex(crypt.digest());
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }

        ret.put("url", url);
        ret.put("jsapi_ticket", jsapi_ticket);
        ret.put("nonceStr", nonce_str);
        ret.put("timestamp", timestamp);
        ret.put("signature", signature);

        return ret;
    }

    private static String byteToHex(final byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash)
        {
            formatter.format("%02x", b);
        }
        String result = formatter.toString();
        formatter.close();
        return result;
    }

    private static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }

    private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }

    public static void getCmdOutput(String[] cmd) {
        Runtime runtime = Runtime.getRuntime();
        Process process = null;
        try {
//            BufferedReader br = new BufferedReader(new InputStreamReader(runtime.exec(cmd).getInputStream()));
            process = runtime.exec("python C:\\Users\\174505131\\Desktop\\add.py 1 2");
        } catch (IOException e) {
            e.printStackTrace();
        }

        String charsetName = "GB2312";
        InputStreamReader reader;

        try {
            reader = new InputStreamReader(process.getInputStream(), charsetName);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return;
        }
        BufferedReader bufferedReader = new BufferedReader(reader);

        String line;
        try {
            while (true) {
                line = bufferedReader.readLine();
                if (line == null)
                    break;
                System.out.println(line);
            }
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


